package com.course.db;

import java.sql.ResultSet;

public class Board extends ExecuteDB {
	
	private int Id;
	private String title;
	private String content;
	private String date;
	private String strSql;
	
	public Board()
	{
		this.Id = 0;
		this.title = "";
		this.content = "";
		this.date = "";
		this.strSql = "";
	}
	
	public String CountSql()
	{
		this.strSql = "select count(*) from board";
		return this.strSql;
	} 
	
	//add a ChooseCourse
	public boolean add()
	{

		this.strSql="insert into Board ";
		this.strSql=this.strSql + "(title,content,date) ";
		this.strSql=this.strSql + "values('" + this.title + "','" + this.content + "','" + this.date + "')";
		System.out.println(this.strSql);
		boolean isAdd = super.exeSql(this.strSql);
        return isAdd;
	}
	
	public int lastid()
	{
		int lid = 0;
		this.strSql = "select last_insert_id() as id from Board limit 1";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
	
	//show latest infomation
    public ResultSet latest_info()
	{
		this.strSql="select * from board where Id in (select max(Id) from board)";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return rs;
	}
    
    public int latest_boardid()
	{
		int lid = 0;
		this.strSql = "select max(Id) as board_id from board";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("board_id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
    
  //initialize CourseTerm information
 	public boolean init()
	{
		this.strSql="select * from board where Id=";
		this.strSql=this.strSql + "'" + this.Id + "'";

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				this.title=rs.getString("title");
				this.content=rs.getString("content");
				this.date=rs.getString("date");
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
	}

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}

}
